package com.ruoyi.web.controller.business;

import com.ruoyi.business.domain.entity.TbProduct;
import com.ruoyi.business.iService.ITbProductService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 商品Controller
 *
 * @author ruoyi
 * @date 2025-07-26
 */
@Anonymous
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/business/product")
public class TbProductController extends BaseController {
    private final ITbProductService tbProductService;

    /**
     * 查询商品列表
     */
//    @PreAuthorize("@ss.hasPermi('business:product:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbProduct tbProduct) {
        return tbProductService.selectTbProductPage(tbProduct, TableSupport.getPageDomain());
    }

    /**
     * 导出商品列表
     */
    @PreAuthorize("@ss.hasPermi('business:product:export')")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbProduct tbProduct) {
        List<TbProduct> list = tbProductService.selectTbProductList(tbProduct);
        ExcelUtil<TbProduct> util = new ExcelUtil<TbProduct>(TbProduct.class);
        util.exportExcel(response, list, "商品数据");
    }

    /**
     * 获取商品详细信息
     */
//    @PreAuthorize("@ss.hasPermi('business:product:query')")
    @GetMapping(value = "/{productId}")
    public AjaxResult getInfo(@PathVariable("productId") Long productId) {
        return success(tbProductService.getById(productId));
    }

    /**
     * 新增商品
     */
//    @PreAuthorize("@ss.hasPermi('business:product:add')")
//    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbProduct tbProduct) {
        return toAjax(tbProductService.save(tbProduct));
    }

    /**
     * 修改商品
     */
    @PreAuthorize("@ss.hasPermi('business:product:edit')")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbProduct tbProduct) {
        return toAjax(tbProductService.updateById(tbProduct));
    }

    /**
     * 删除商品
     */
    @PreAuthorize("@ss.hasPermi('business:product:remove')")
    @Log(title = "商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{productIds}")
    public AjaxResult remove(@PathVariable Long[] productIds) {
        return toAjax(tbProductService.removeByIds(Arrays.asList(productIds)));
    }
}
